package models

import (
	"github.com/go-redis/redis"
	"sport_booking/dao"
	"sport_booking/utils/message"
	"strconv"
	"time"
)

/*
新增验证码
*/
func SetCode(codeKey string, codeValue int, timeNum time.Duration) int {
	err := dao.RDB.Set(codeKey, codeValue, time.Minute*timeNum).Err()
	if err != nil {
		return message.ErrorAddAuthCode
	}
	return message.SUCCESS
}

/*
根据key查找验证码
*/
func GetCodeValue(codeKey string) (int, int) {
	result, err := dao.RDB.Get(codeKey).Result()
	if err == redis.Nil {
		return 0, message.ErrorNoAuthKey
	} else if err != nil {
		return 0, message.ErrorGetAuthCode
	}
	code, err := strconv.Atoi(result)
	if err != nil {
		return 0, message.ErrorAuthCodeConv
	}
	return code, message.SUCCESS
}

/*
根据key删除验证码
*/
func RemoveCodeValue(codeKey string) {
	dao.RDB.Del(codeKey)
}
